package com.sskj.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

public class AppManager {

    private Stack<Activity> activityStack;

    private static AppManager instance;

    public AppManager() {
        activityStack = new Stack<>();
    }

    public static AppManager getInstance() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }


    public void add(Activity activity) {
        activityStack.add(activity);
    }

    public void remove(Activity activity) {
        activityStack.remove(activity);
    }

    public void finish(Activity activity) {
        if (activity != null) {
            activity.finish();
            activityStack.remove(activity);
        }
    }

    public Activity getCurrentActivity() {
        return activityStack.lastElement();
    }


    public void finishAll() {
        for (Activity activity : activityStack) {
            activity.finish();
        }
    }

    public void exit(Context context) {
        try {

            for (Activity activity : activityStack) {
                activity.finish();
            }
            activityStack.clear();
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }

    /**
     * 退出应用程序
     */

    public Stack<Activity> getAllActivities() {
        return activityStack;
    }
}
